//
// Created by v on 19-4-29.
//

#include "CMFifoHandler.h"

int CMFifoHandler::open(std::string path)
{
    int ret = cm_fifo_w_open(&writeHandle, path.c_str());
    if (ret < 0)
    {
        LOGFIFO("fifo open write fail ret = %d\n", ret);
        return -1;
    }
    LOGFIFO("fifo open [%s] success %p\n", path.c_str(), writeHandle);
    return 0;
}

int CMFifoHandler::close()
{
    if (writeHandle)
    {
        cm_fifo_w_close(&writeHandle);
    }
    return 0;
}

int CMFifoHandler::write(std::string cmd, std::string data)
{
    if (!writeHandle)
        return 0;
    std::string senddata = "0 0 ";
    senddata.append(cmd + " ");
    senddata.append(data);
    int ret = cm_fifo_w_write(writeHandle, senddata.c_str(), senddata.length());
    if (ret <= 0)
    {
        LOGFIFO("fifo write fail ret = %d\n", ret);
        return -1;
    }
    return ret;
}